# -*- coding: utf-8 -*-
#
# Copyright © 2010 Benedikt Eger
#
# This file is part of top-news-buzz.
#
# top-news-buzz is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# top-news-buzz is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with top-news-buzz.  If not, see <http://www.gnu.org/licenses/>.

import os

from google.appengine.api import memcache

from topnewsbuzz.controller import MainPage
from topnewsbuzz.config import ConfigValue, MetricConfigEntry, Config
from topnewsbuzz.feed import Feed
from topnewsbuzz.metrics import MetricHelper

class AdminPage(MainPage):

    def service(self):
        memcache.flush_all()

        self.template_url = '../admin/config.html'        


class ConfigAdminPage(AdminPage):

    def service(self):
        super(ConfigAdminPage, self).service()

        config_path = os.path.join(os.path.dirname(__file__), '../admin/main.html')

        self.template_values.update({'config' : Config,
                                     'config_path' : config_path})

    def post(self):
        for config_key in self.request.params:
            config = ConfigValue.get_or_insert(key_name = config_key)
            config.value = self.request.params[config_key]
            config.put()

        self.redirect(self.request.url)

class FeedAdminPage(AdminPage):

    def service(self):
        super(FeedAdminPage, self).service()

        config_path = os.path.join(os.path.dirname(__file__), '../admin/feeds.html')

        self.template_values.update({'feeds' : Config.get_feeds(),
                                     'config_path' : config_path})

    def post(self):
        feed_urls_to_delete = self.request.get_all('feed_url_old')
        feed_urls = self.request.get_all('feed_url')
        feed_boost = self.request.get_all('feed_boost')
        for feed_index in range(len(feed_urls)):
            url = feed_urls[feed_index]
            if url != '':
                feed = Feed.get_or_insert(url, url = url)
                try:
                    boost = feed_boost[feed_index]
                    feed.boost = float(boost)
                except ValueError:
                    feed.boost = 1.0
                feed.put()
                if url in feed_urls_to_delete:
                    feed_urls_to_delete.remove(url)

        for url in feed_urls_to_delete:
            feed = Feed.get_by_key_name(url)
            feed.delete()

        self.redirect(self.request.url)

class MetricAdminPage(AdminPage):

    def service(self):
        super(MetricAdminPage, self).service()

        metrics = MetricHelper.load_metrics()

        config_path = os.path.join(os.path.dirname(__file__), '../admin/metrics.html')

        self.template_values.update({'metrics' : metrics,
                                     'config_path' : config_path,
                                     'MetricConfigEntry' : MetricConfigEntry })


    def post(self):
        for metric_config_key in self.request.params:
            MetricConfigEntry.store(metric_config_key, self.request.params[metric_config_key])

        self.redirect(self.request.url)
